const { Controller } = require('dx-router-admin');

module.exports = class Index extends Controller {
	constructor(ctx) {
		super(ctx);
	}
	async index() {
		// 先取本地
		const info = await this.$dx.service.familyOrigin.where('xing', 'like', '宋').find();
		if (info) {
			return this.$dx.success('获取成功', info);
		}
		// 到API取
		const url = `https://www.maitanbang.com/apis/mtbxmqy/?key=viYwgwrM6FyVOEJA5nx3J72ZGE&xing=宋`;
		const http = new this.$dx.Curl();
		const res = await http.get(url);
		if (res.code === 200) {
			await this.$dx.service.familyOrigin.insert(res.data[0]);
			return this.$dx.success('获取成功', res.data[0]);
		} else {
			return this.$dx.error(res.msg);
		}
	}
};
